<template>
  <el-aside :width="width">
    <el-menu
      background-color="#545c64"
      text-color="#fff"
      :collapse="isCollapse"
      :collapse-transition="true"
      :default-active="activeMenu"
    >
      <h3 v-show="!isCollapse">通用后台管理系统</h3>
      <h3 v-show="isCollapse">后台</h3>
      <el-menu-item v-for="item in noChildren"
                    :index="item.path" :key="item.path"
                    @click="handMenuItem(item)">
        <component class="icons" :is="item.icon"/>
        <span>{{ item.label }}</span>
      </el-menu-item>
      <el-sub-menu v-for="item in hasChildren"
                   :index="item.path" :key="item.path">
        <template #title>
          <component class="icons" :is="item.icon"/>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item v-for="(subItem, index) in item.children"
                        :index="subItem.path" :key="subItem.path + '-' + index"
                        @click="handMenuItem(subItem)">
            <component class="icons" :is="subItem.icon"/>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script setup lang="ts">
import {computed, toRef} from 'vue'
import { userCollapseStore } from '@/store/collapse'
import { useRouter, useRoute } from 'vue-router'
import type { Item } from '@/assets/type/commonType'
import { useTagsStore } from '@/store/tags'
import { userLoginStore } from '@/store/userLogin'

const loginStore = userLoginStore()
const list = toRef<Item[]>(loginStore.menuList)

const noChildren = computed(() => list.value.filter(item => !item.children))
const hasChildren = computed(() => list.value.filter(item => item.children))

const store = userCollapseStore()
const isCollapse = computed(() => store.collapse)
const width = computed(() => store.collapse ? '64px' : '180px')

const router = useRouter()
const route = useRoute()
const activeMenu = computed(() => route.path)
const { selectMenu } = useTagsStore()
const handMenuItem = (item) => {
  router.push(item.path)
  selectMenu(item)
}
</script>

<style scoped lang="less">
.el-aside{
  transition: all 0.4s;
}

.icons{
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.el-aside{
  height: 100%;
  background-color: #545c64;
}
.el-menu{
  border-right: none;
  h3{
    line-height: 48px;
    text-align: center;
    color: #fff;
  }
}
</style>